package com.ltv.xiangqiplugin.views;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.ltv.xiangqiplugin.controllers.GameActionControl;
import com.ltv.xiangqiplugin.models.BoardSizeEnum;
import com.ltv.xiangqiplugin.models.BoardSizeObserver;

public class GameActionView extends JPanel implements BoardSizeObserver {
	private static GameActionView instance;
	
	private JButton reverse;
	private JButton first;
	private JButton prev;
	private JButton next;
	private JButton last;
	private JButton rollback;
	private JButton html;
	
	private GameActionView() {
	    JPanel panel = new JPanel();
	    
	    reverse = new JButton("");
	    ImageIcon reverseIcon = new ImageIcon(getClass().getResource("/buttons/reverse.png"));
	    reverse.setIcon(reverseIcon);
	    reverse.setActionCommand("REVERSE");
	    reverse.addActionListener(GameActionControl.getInstance());
	    reverse.setPreferredSize(new Dimension(16, 16));
	    panel.add(reverse);
	    reverse.setToolTipText("Reverse");
	    
	    first = new JButton("");
	    ImageIcon firstIcon = new ImageIcon(getClass().getResource("/buttons/first.png"));
	    first.setIcon(firstIcon);
	    first.setActionCommand("FIRST");
	    first.addActionListener(GameActionControl.getInstance());
	    panel.add(first);
	    first.setToolTipText("Move First");
	    
	    prev = new JButton("");
	    ImageIcon prevIcon = new ImageIcon(getClass().getResource("/buttons/prev.png"));
	    prev.setIcon(prevIcon);
	    prev.setActionCommand("PREV");
	    prev.addActionListener(GameActionControl.getInstance());
	    panel.add(prev);
	    prev.setToolTipText("Move Previous");
	    
	    next = new JButton("");
	    ImageIcon nextIcon = new ImageIcon(getClass().getResource("/buttons/next.png"));
	    next.setIcon(nextIcon);
	    next.setActionCommand("NEXT");
	    next.addActionListener(GameActionControl.getInstance());
	    panel.add(next);
	    next.setToolTipText("Move Next");
	    
	    last = new JButton("");
	    ImageIcon lastIcon = new ImageIcon(getClass().getResource("/buttons/last.png"));
	    last.setIcon(lastIcon);
	    last.setActionCommand("LAST");
	    last.addActionListener(GameActionControl.getInstance());
	    panel.add(last);
	    last.setToolTipText("Move Last");
	    
	    ImageIcon rollbackIcon = new ImageIcon(getClass().getResource("/buttons/refresh.png"));
	    rollback = new JButton("");
	    rollback.setIcon(rollbackIcon);
	    rollback.setEnabled(false);
	    rollback.setActionCommand("ROLLBACK");
	    rollback.addActionListener(GameActionControl.getInstance());
	    panel.add(rollback);
	    rollback.setToolTipText("Refresh");
	    
	    ImageIcon htmlIcon = new ImageIcon(getClass().getResource("/buttons/html.png"));
	    html = new JButton("");
	    html.setIcon(htmlIcon);
	    html.setActionCommand("HTML");
	    html.addActionListener(GameActionControl.getInstance());
	    panel.add(html);
	    html.setToolTipText("Export to HTML");
	    
	    this.add(panel);
	    this.add(new JPanel());
	    
	}

	public static GameActionView getInstance() {
		if (instance == null) {
			instance = new GameActionView();
		}
		
		return instance;
	}
	
	public void enableRollbackButton() {
		rollback.setEnabled(true);
	}
	
	public void disableRollbackButton() {
		rollback.setEnabled(false);
	}
	
	public boolean isEnableRollback() {
		return rollback.isEnabled();
	}

	@Override
	public void boardSizeChanged(BoardSizeEnum size) {
		resizeButtons(size);
	}
	
	private void resizeButtons(BoardSizeEnum size) {
		if (size == BoardSizeEnum.SMALL) {
			int w = 32;
			int h = 16;
			reverse.setPreferredSize(new Dimension(w, h));
			first.setPreferredSize(new Dimension(w, h));
			prev.setPreferredSize(new Dimension(w, h));
			next.setPreferredSize(new Dimension(w, h));
			last.setPreferredSize(new Dimension(w, h));
			rollback.setPreferredSize(new Dimension(w, h));
		} else if (size == BoardSizeEnum.MEDIUM) {
			int w = 40;
			int h = 24;
			reverse.setPreferredSize(new Dimension(w, h));
			first.setPreferredSize(new Dimension(w, h));
			prev.setPreferredSize(new Dimension(w, h));
			next.setPreferredSize(new Dimension(w, h));
			last.setPreferredSize(new Dimension(w, h));
			rollback.setPreferredSize(new Dimension(w, h));
		} else if (size == BoardSizeEnum.LARGE) {
			int w = 48;
			int h = 32;
			reverse.setPreferredSize(new Dimension(w, h));
			first.setPreferredSize(new Dimension(w, h));
			prev.setPreferredSize(new Dimension(w, h));
			next.setPreferredSize(new Dimension(w, h));
			last.setPreferredSize(new Dimension(w, h));
			rollback.setPreferredSize(new Dimension(w, h));
		}
	}
}
